<?php
require_once 'Database.php';
$db = new Database();
$db->connect();
$db->logEvent('ServerHeaderDOMDocument top', 51, 'after require_once Database.php');
class ServerHeaderDOMDocument extends DOMDocument
{
    public $clientUsername;
    public $clientPassword;
    public $callerUsername;
    public $callerPassword;
    public $hash;

    /*
     * read the XML from the current DOM document and create username, password,
     * and hash fields to be used by other classes.
     */
    public function loadXML($source, $options = 0)
    {
        $db = new Database();
        $db->connect();
        $db->logEvent(__METHOD__, 21, $source);
        try 
        {
           /**
            * The @ symbol is added to the evaluation below to suppress a 
            * warning about empty string parameters that was being kicked and 
            * only showed up in the dynamic runtime generation of the wsdl, thus 
            * preventing anyone from getting the wsdl via the <URL>?wsdl 
            * construct.
            */
            if (@parent::loadXml($source))
            {
                return $this->parseHeader();
            }
            else
            {
                return FALSE; // no xml, return false;
            }
        }
        catch (Exception $ee)	
        {
            $db->logEvent(__METHOD__, 1, 'Exception: ' . $ee->getMessage());
            return FALSE;
        }
    }

    /*
     * Parse header values into class properties/fields
     */
    public function parseHeader()
    {
    	try 
    	{
            $db = new Database();
            $db->connect();
            $db->logEvent(__METHOD__, 24, 'enter method');
            
            $xpath = new DOMXPath($this);
            $xpath->registerNamespace('ns2',
                    'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd');
            $xpath->registerNamespace('SOAP-ENV', 'http://schemas.xmlsoap.org/soap/envelope/');

            $xPathPrefix = 
                    'string(/SOAP-ENV:Envelope/SOAP-ENV:Header/ns2:Security/';

            $this->hash = $xpath->evaluate($xPathPrefix  . 'ns2:BinarySecurityToken)');
            $db->logEvent(__METHOD__, 55, 'hash = ' . $this->hash);
            
            $userTokenXpath =$xPathPrefix . 'ns2:UsernameToken[position()=1]/';
            $this->clientUsername =  $xpath->evaluate($userTokenXpath . 'ns2:Username)');
            $db->logEvent(__METHOD__, 55, 'client = ' . $this->clientUsername);
            
            $this->clientPassword =  $xpath->evaluate($userTokenXpath . 'ns2:Password)');
            $db->logEvent(__METHOD__, 55, 'clientPassword = ' . $this->clientPassword);

            $userTokenXpath = str_replace('1', '2', $userTokenXpath);
            $this->callerUsername = $xpath->evaluate($userTokenXpath . 'ns2:Username)');
            $db->logEvent(__METHOD__, 55, 'caller = ' . $this->callerUsername);

            $this->callerPassword = $xpath->evaluate($userTokenXpath . 'ns2:Password)');
            $db->logEvent(__METHOD__, 55, 'callerPassowrd = ' .  $this->callerPassword);

            return TRUE;
    	}
    	catch (Exception $ee)
    	{
            $db = new Database();
            $db->connect();
            $db->logEvent(__METHOD__, 1, $ee->getMessage());
            return FALSE;
    	}
    }
}
?>